package com.TTBook.utils.pages;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * 
 * @author Frank
 * 
 * <b>Class Description:</b><br>
 * 分页包装类，用来包装查询返回的结果集，结果集中的元素为Object。
 * 注意：此分页包装类只用在数据库分页的情景。当pageIndex和pageSize均为-1时，表示查询全部。
 */
public class DatabasePaginatedList<T> implements PaginatedList<T> {

	private int pageIndex;
	private int pageSize;
	private int totalCount;
	private int pageCount;

	private List<T> pageList;

	private static final long serialVersionUID = 1L;

	public DatabasePaginatedList(int pageIndex, int pageSize) {
		this(pageIndex, pageSize, -1, (List<T>) EMPTY_LIST);
	}

	public DatabasePaginatedList(int pageIndex, int pageSize,
			List<T> pageList) {
		this(pageIndex, pageSize, -1, pageList);
	}

	public DatabasePaginatedList(int pageIndex, int pageSize, int totalCount,
			List<T> pageList) {
		if (pageIndex < 0)
			this.pageIndex = -1;
		else
			this.pageIndex = pageIndex;

		if (pageSize < 0)
			this.pageSize = -1;
		else
			this.pageSize = pageSize;

		if (totalCount < 0)
			this.totalCount = -1;
		else
			this.totalCount = totalCount;

		this.pageList = pageList;
		
		this.pageCount = this.totalCount / this.pageSize
				  + (this.totalCount % this.pageSize == 0 ? 0 : 1);
		
		if (totalCount == 0) {
			this.pageIndex = 1;
		} else {
			if (pageIndex > pageCount)
				this.pageIndex = pageCount;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.i2f.i2shopping.util.pages.PagedListWrapper#getPageIndex()
	 */
	@Override
	public int getPageIndex() {
		return pageIndex;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.i2f.i2shopping.util.pages.PagedListWrapper#getPageSize()
	 */
	@Override
	public int getPageSize() {
		return pageSize;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.i2f.i2shopping.util.pages.PagedListWrapper#getPageCount()
	 */
	@Override
	public int getPageCount() {
		return this.pageCount;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.i2f.i2shopping.util.pages.PagedListWrapper#getTotalCount()
	 */
	@Override
	public int getTotalCount() {
		return totalCount;
	}

	
	
	/* (non-Javadoc)
	 * @see com.i2f.i2shopping.util.pages.PaginatedList#getPageList()
	 */
	@Override
	public List<? extends Object> getPageList() {
		return this.pageList;
	}
	
	/* (non-Javadoc)
	 * @see java.util.List#size()
	 */
	@Override
	public int size() {
		return this.pageList.size();
	}

	/* (non-Javadoc)
	 * @see java.util.List#isEmpty()
	 */
	@Override
	public boolean isEmpty() {
		return this.pageList.isEmpty();
	}

	/* (non-Javadoc)
	 * @see java.util.List#contains(java.lang.Object)
	 */
	@Override
	public boolean contains(Object o) {
		return  this.pageList.contains(o);
	}

	/* (non-Javadoc)
	 * @see java.util.List#iterator()
	 */
	@Override
	public Iterator<T> iterator() {
		return this.pageList.iterator();
	}

	/* (non-Javadoc)
	 * @see java.util.List#toArray()
	 */
	@Override
	public Object[] toArray() {
		return this.pageList.toArray();
	}

	/* (non-Javadoc)
	 * @see java.util.List#toArray(T[])
	 */
	@Override
	public <T> T[] toArray(T[] a) {
		return this.pageList.toArray(a);
	}

	/* (non-Javadoc)
	 * @see java.util.List#add(java.lang.Object)
	 */
	@Override
	public boolean add(T e) {
		return this.pageList.add(e);
	}

	/* (non-Javadoc)
	 * @see java.util.List#remove(java.lang.Object)
	 */
	@Override
	public boolean remove(Object o) {
		return this.pageList.remove(o);
	}

	/* (non-Javadoc)
	 * @see java.util.List#containsAll(java.util.Collection)
	 */
	@Override
	public boolean containsAll(Collection<?> c) {
		return this.pageList.containsAll(c);
	}

	/* (non-Javadoc)
	 * @see java.util.List#addAll(java.util.Collection)
	 */
	@Override
	public boolean addAll(Collection<? extends T> c) {
		return this.pageList.addAll(c);
	}

	/* (non-Javadoc)
	 * @see java.util.List#addAll(int, java.util.Collection)
	 */
	@Override
	public boolean addAll(int index, Collection<? extends T> c) {
		return this.pageList.addAll(index,c);
	}

	/* (non-Javadoc)
	 * @see java.util.List#removeAll(java.util.Collection)
	 */
	@Override
	public boolean removeAll(Collection<?> c) {
		return this.pageList.removeAll(c);
	}

	/* (non-Javadoc)
	 * @see java.util.List#retainAll(java.util.Collection)
	 */
	@Override
	public boolean retainAll(Collection<?> c) {
		return this.pageList.retainAll(c);
	}

	/* (non-Javadoc)
	 * @see java.util.List#clear()
	 */
	@Override
	public void clear() {
		 this.pageList.clear();
	}

	/* (non-Javadoc)
	 * @see java.util.List#get(int)
	 */
	@Override
	public T get(int index) {
		return this.pageList.get(index);
	}

	/* (non-Javadoc)
	 * @see java.util.List#set(int, java.lang.Object)
	 */
	@Override
	public T set(int index, T element) {
		return this.pageList.set(index,element);
	}

	/* (non-Javadoc)
	 * @see java.util.List#add(int, java.lang.Object)
	 */
	@Override
	public void add(int index, T element) {
		 this.pageList.add(index,element);
	}

	/* (non-Javadoc)
	 * @see java.util.List#remove(int)
	 */
	@Override
	public T remove(int index) {
		return  this.pageList.remove(index);
	}

	/* (non-Javadoc)
	 * @see java.util.List#indexOf(java.lang.Object)
	 */
	@Override
	public int indexOf(Object o) {
		return  this.pageList.indexOf(o);
	}

	/* (non-Javadoc)
	 * @see java.util.List#lastIndexOf(java.lang.Object)
	 */
	@Override
	public int lastIndexOf(Object o) {
		 return  this.pageList.lastIndexOf(o);
	}

	/* (non-Javadoc)
	 * @see java.util.List#listIterator()
	 */
	@Override
	public ListIterator<T> listIterator() {
		return this.pageList.listIterator();
	}

	/* (non-Javadoc)
	 * @see java.util.List#listIterator(int)
	 */
	@Override
	public ListIterator<T> listIterator(int index) {
		return this.pageList.listIterator(index);
	}

	/* (non-Javadoc)
	 * @see java.util.List#subList(int, int)
	 */
	@Override
	public List<T> subList(int fromIndex, int toIndex) {
		return this.pageList.subList(fromIndex,toIndex);
	}



	

}
